package com.loooooo.pm2024.utils;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.*;

public class ExcelUtils {


    public static List<Map<String, String>> read(File file) {
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            return read(inputStream, file.getName().toLowerCase().endsWith(".xlsx"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static List<Map<String, String>> read(InputStream inputStream) {
        return read(inputStream, true);
    }

    public static List<Map<String, String>> read(InputStream inputStream, boolean highVersion) {
        Workbook wb = null;
        try {
            if (highVersion) {
                wb = new XSSFWorkbook(inputStream);
            } else {
                wb = new HSSFWorkbook(inputStream);
            }
            Sheet sheet = wb.getSheetAt(0);
            List<Map<String, String>> list = new ArrayList<>();
            Row headRow = selectHeadRow(sheet);// sheet.getRow(sheet.getFirstRowNum());
            if (headRow == null) return null;
            for (int r = headRow.getRowNum() + 1; r <= sheet.getLastRowNum(); r++) {
                Row row = sheet.getRow(r);
                if (row == null) continue;
                Map<String, String> data = new HashMap<>();
                for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
                    Cell cell = row.getCell(c);
                    if (cell == null) continue;
                    Cell head = headRow.getCell(c);
                    if (head == null) continue;
                    data.put(head.toString(), cell.toString());
                    System.out.println(String.format("(%s,%s) %s", r, c, cell.toString()));
                }
                if (data.size() > 0) list.add(data);
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static List<Map<Integer, String>> readDataFromSheet(InputStream inputStream, boolean highVersion, String sheetName, int startRow) {
        Workbook wb = null;
        try {
            if (highVersion) {
                wb = new XSSFWorkbook(inputStream);
            } else {
                wb = new HSSFWorkbook(inputStream);
            }
            Sheet sheet = wb.getSheet(sheetName);
            return readDataFromSheet(sheet, startRow);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static List<Map<Integer, String>> readDataFromSheet(Sheet sheet, int startRow) {
        List<Map<Integer, String>> list = new ArrayList<>();
        for (int r = startRow; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);
            if (row == null) continue;
            Map<Integer, String> data = new HashMap<>();
            for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
                Cell cell = row.getCell(c);
                if (cell == null) continue;
                data.put(c, cell.toString());
            }
            if (data.size() > 0) {
                data.put(-1, "" + (list.size() + 1));
                list.add(data);
            }
        }
        return list;
    }


    static Row selectHeadRow(Sheet sheet) {
        Row headRow = sheet.getRow(sheet.getFirstRowNum());
        if (headRow == null) return null;
        Row headRow2 = sheet.getRow(headRow.getRowNum() + 1);
        if (headRow2 == null) return headRow;
        int cc1 = 0, cc2 = 0;
        for (int c = headRow.getFirstCellNum(); c < headRow.getLastCellNum(); c++) {
            Cell cell = headRow.getCell(c);
            if (cell == null || cell.getCellType() == CellType.BLANK) continue;
            cc1++;
        }
        for (int c = headRow2.getFirstCellNum(); c < headRow2.getLastCellNum(); c++) {
            Cell cell = headRow2.getCell(c);
            if (cell == null || cell.getCellType() == CellType.BLANK) continue;
            cc2++;
        }
        return cc2 > cc1 ? headRow2 : headRow;
    }

    public static void main(String[] args) throws FileNotFoundException {

    }

}
